package com.astar.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@EnableOpenApi
public class Swagger3Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.astar.controller"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(Arrays.asList(SecurityContext.builder()
                                .securityReferences(Arrays.asList(SecurityReference.builder()
                                        .reference("Authorization")
                                        .scopes(new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")})
                                        .build()))
                                .build()))
                .securitySchemes(Arrays.asList(new ApiKey("Authorization", "Authorization", "header")));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("云E办")
                .description("通过此软件实现在线办公")
                .contact(new Contact("weishen","http:localhost:8081/doc.html","123@qq.com"))
                .version("1.0")
                .build();
    }

    //设置请求头信息
    private List<SecurityScheme> securitySchemes(){
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        SecurityScheme securityScheme = new ApiKey("Authorization","Authorization","Header");
        apiKeyList.add(securityScheme);
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts(){
        List<SecurityContext> securityContextList = new ArrayList<>();
        //设置需要登录认证的路径
        securityContextList.add(getContext("/hello/.*"));
        return securityContextList;
    }

    private SecurityContext getContext(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> referenceList = new ArrayList<>();
        AuthorizationScope authorizationScope  = new AuthorizationScope("global","accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0]=authorizationScope;
        referenceList.add(new SecurityReference("Authorization",authorizationScopes));
        return referenceList;
    }
}
